/*
* PureMVC Logger v1.0
* Copyright (c) 2009 We Are Cloud.
* Author: Yannick Chaze, Nicolas Raspal
*
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0.
*/

package com.wearecloud.logs
{
	/**
	 * The application logger is a use sample of the application logger
	 * It catches the messages and trace them
	 * It has three differents levels of logging
	 */
	
	public class ApplicationLogger
	{
		// -------------------------------------------------
		// Constants
		// -------------------------------------------------
		
		public static const LOG_MESSAGE:String = 'logMessage'
		
		/**
		 * Specify the highest level of log
		 * All messages are logged
		 */ 
		public static const ALL:Number = 0;
		
		/**
		 * Specify the intermediate level of log
		 * Only message from the application are logged
		 *  The messages from PureMVC are not.
		 */
		public static const NO_PUREMVC:Number = 1;
		
		/**
		 * Specify the lowest level of log
		 * Only the important part of log are caught
		 */
		public static const LIGHT:Number = 2;
		
		// -------------------------------------------------
		// Class Variables
		// -------------------------------------------------
		
		/**
		 * @private
		 * 
		 * Specify if the logger is enabled in the application or not
		 */
		private static var _enabled:Boolean = false
		
		/**
		 * @private
		 * 
		 * Specify if the the current level of log used by the application
		 */
		private static var _level:Number = 0;
		
		/**
		 * @private
		 * 
		 * Singleton instance of the application logger
		 */
		private static var _instance:ApplicationLogger
		
		// -------------------------------------------------
		// Instance Variables
		// -------------------------------------------------
		
		/**
		 * @private
		 * 
		 * Specify wich function must be used to log messages
		 * depending of environment (eg. Development, production)
		 */
		private var targetFunction:Function
		
		// -------------------------------------------------
		// Constructor
		// -------------------------------------------------
		
		public function ApplicationLogger(blocker:SingletonBlocker)
		{
			super();
		}
		
		/**
		 * Initialize correctly the singleton application logger
		 * By default the log level is ALL so all messages are
		 * caught and logged
		 * 
		 * In development mode you have to specify the development
		 * message function as target message.
		 * 
		 */
		public static function init():void
		{
			if(!_instance)
			{
				_instance = new ApplicationLogger(new SingletonBlocker);
				level = ALL;
				_instance.targetFunction = _instance.developmentMessage // standard trace
				_enabled = true;
			}
		}
		
		// -------------------------------------------------
		// Getters and setters
		// -------------------------------------------------
		
		/**
		 * @private
		 */ 
		public static function set enabled(value:Boolean):void
		{
			_enabled = value
		}
		
		/**
		 * Get if the logger is activated.
		 */ 
		public static function get enabled():Boolean
		{
			return _enabled;
		}
		
		/**
		 * @private
		 */ 
		public static function set level(value:Number):void
		{
			if (value == ALL || value == NO_PUREMVC || value == LIGHT)
			{
				_level = value;
			}
		}
		
		/**
		 * Get if the level of log.
		 */ 
		public static function get level():Number
		{
			return _level;
		}
		
		// -------------------------------------------------
		// Logger
		// -------------------------------------------------
		
		/**
		 * Logs the PureMVC messages caught by the 
		 * <code>Log</code> class used by the PureMVC logger
		 */ 
		public static function puremvcmessage(value:String):void
		{
			if(!_enabled || level == NO_PUREMVC) return;
			
			var log:String = "[ PureMVC ] :: ";
			log+=value;
			_instance.targetFunction(log);
		}
		
		/**
		 * Logs the application messages
		 */ 
		public static function message(value:Object):void
		{
			if(!_enabled) return;
			var log:String = "";
			if (value)
				log += value.toString();
			else
				log += value;
			_instance.targetFunction(log);
		}
		
		// -------------------------------------------------
		// Target message functions
		// -------------------------------------------------
		
		/**
		 * Target log function for development environment
		 */ 
		private function developmentMessage(message:String):void
		{
			trace(message)
		}
		
		/**
		 * Target log function for production environment
		 */ 
		private function productionMessage(message:String):void
		{
			// place your production dispatch method for logger here
		}
	}
}

/**
 * The <code>SingletonBlocker</code> class is a private inner class
 * used to guarantee that the <code>ApplicationLogger</code> is unique.
 */
class SingletonBlocker{}